package main.java.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionSortDemo3 {
    public static void main(String[] args) {
        //创建一个Point对象
        Point p1 = new Point(1, 2);
        Point p2 = new Point(3, 4);
        //创建集合对象
        List<Point> list = new ArrayList<>();
        //添加元素
        list.add(p1);
        list.add(p2);

        //调用Collections.sort(List<T>list,Comparator cc)方法对Point对象排序
        //方法中的Comparator对象为比较器对象，适用于更加灵活的排序场景
        Comparator<Point> comparator1=new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                return o1.getX()-o2.getX();
            }
        };
        Comparator<Point> comparator2=new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                return o1.getY()-o2.getY();
            }
        };
        Collections.sort(list,comparator1);
        System.out.println(list);
        Collections.sort(list,comparator2);
        System.out.println(list);
    };
}

